package com.chengcang.admin.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chengcang.admin.modules.sys.dao.SysAreaDao;
import com.chengcang.admin.modules.sys.dto.SysAreaDTO;
import com.chengcang.admin.modules.sys.entity.SysAreaEntity;
import com.chengcang.admin.modules.sys.service.SysAreaService;
import com.chengcang.common.service.impl.CrudServiceImpl;
import com.chengcang.common.utils.ConvertUtils;
import com.chengcang.common.utils.TreeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 区域表
 *
 * @author zhanli
 * @since 1.0.0 2023-01-10
 */
@Service
public class SysAreaServiceImpl extends CrudServiceImpl<SysAreaDao, SysAreaEntity, SysAreaDTO> implements SysAreaService {

    @Override
    public QueryWrapper<SysAreaEntity> getWrapper(Map<String, Object> params) {
        String id = (String) params.get("id");

        QueryWrapper<SysAreaEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<SysAreaDTO> tree() {
        List<SysAreaEntity> sysAreaEntities = baseDao.selectList(new QueryWrapper<>());

        List<SysAreaDTO> dtoList = ConvertUtils.sourceToTarget(sysAreaEntities, SysAreaDTO.class);

        return TreeUtils.build(dtoList);
    }
}